<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic
  PUBLIC "-//OASIS//DTD DITA Composite//EN" "ditabase.dtd">
<topic id="topic1">
  <title id="dt20941">INSERT</title>
  <body>
    <p id="sql_command_desc">Creates new rows in a table.</p>
    <section id="section2">
      <title>Synopsis</title>
      <codeblock id="sql_command_synopsis">[ WITH [ RECURSIVE ] <varname>with_query</varname> [, ...] ]
INSERT INTO <varname>table</varname> [( <varname>column</varname> [, ...] )]
   {DEFAULT VALUES | VALUES ( {<varname>expression</varname> | DEFAULT} [, ...] ) [, ...] | <varname>query</varname>}
   [RETURNING * | <varname>output_expression</varname> [[AS] <varname>output_name</varname>] [, ...]]</codeblock>
    </section>
    <section id="section3">
      <title>Description</title>
      <p><codeph>INSERT</codeph> inserts new rows into a table. One can insert one or more rows
        specified by value expressions, or zero or more rows resulting from a query.</p>
      <p>The target column names may be listed in any order. If no list of column names is given at
        all, the default is the columns of the table in their declared order. The values supplied by
        the <codeph>VALUES</codeph> clause or query are associated with the explicit or implicit
        column list left-to-right. </p>
      <p>Each column not present in the explicit or implicit column list will be filled with a
        default value, either its declared default value or null if there is no default. </p>
      <p>If the expression for any column is not of the correct data type, automatic type conversion
        will be attempted.</p>
      <p>The optional <codeph>RETURNING</codeph> clause causes <codeph>INSERT</codeph> to compute
        and return value(s) based on each row actually inserted. This is primarily useful for
        obtaining values that were supplied by defaults, such as a serial sequence number. However,
        any expression using the table's columns is allowed. The syntax of the
          <codeph>RETURNING</codeph> list is identical to that of the output list of
          <codeph>SELECT</codeph>.</p>
      <p>You must have <codeph>INSERT</codeph> privilege on a table in order to insert into it.
        When a column list is specified, you need <codeph>INSERT</codeph> privilege only on the listed columns.
        Use of the <codeph>RETURNING</codeph> clause requires <codeph>SELECT</codeph>
        privilege on all columns mentioned in <codeph>RETURNING</codeph>.  If you provide
        a <varname>query</varname> to insert rows from a query, you must have
        <codeph>SELECT</codeph> privilege on any table or column referenced in the
         query.</p>
      <sectiondiv id="section4">
        <b>Outputs</b>
        <p>On successful completion, an <codeph>INSERT</codeph> command returns a command tag of the
          form:</p><codeblock>INSERT <varname>oid</varname> <varname>count</varname></codeblock><p>The
            <varname>count</varname> is the number of rows inserted. If count is exactly one, and
          the target table has OIDs, then <varname>oid</varname> is the OID assigned to the inserted
          row. Otherwise <varname>oid</varname> is zero.</p>
      </sectiondiv>
    </section>
    <section id="section5">
      <title>Parameters</title>
      <parml>
        <plentry>
          <pt><varname>with_query</varname></pt>
          <pd>The <codeph>WITH</codeph> clause allows you to specify one or more subqueries that can
            be referenced by name in the <codeph>INSERT</codeph> query. </pd>
          <pd>For an <codeph>INSERT</codeph> command that includes a <codeph>WITH</codeph> clause,
            the clause can only contain <codeph>SELECT</codeph> statements, the
              <codeph>WITH</codeph> clause cannot contain a data-modifying command
              (<codeph>INSERT</codeph>, <codeph>UPDATE</codeph>, or <codeph>DELETE</codeph>). </pd>
          <pd>It is possible for the query (<codeph>SELECT</codeph> statement) to also contain a
              <codeph>WITH</codeph> clause. In such a case both sets of
              <varname>with_query</varname> can be referenced within the <codeph>INSERT</codeph>
            query, but the second one takes precedence since it is more closely nested.</pd>
          <pd>See <xref href="../../admin_guide/query/topics/CTE-query.html#topic_zhs_r1s_w1b" format="html" scope="external">WITH Queries (Common Table Expressions)
</xref> and
                <codeph><xref href="SELECT.xml#topic1"/></codeph> for details. </pd>
        </plentry>
        <plentry>
          <pt>
            <varname>table</varname>
          </pt>
          <pd>The name (optionally schema-qualified) of an existing table.</pd>
        </plentry>
        <plentry>
          <pt>
            <varname>column</varname>
          </pt>
          <pd>The name of a column in table. The column name can be qualified with a subfield name
            or array subscript, if needed. (Inserting into only some fields of a composite column
            leaves the other fields null.) </pd>
        </plentry>
        <plentry>
          <pt>DEFAULT VALUES</pt>
          <pd>All columns will be filled with their default values.</pd>
        </plentry>
        <plentry>
          <pt>
            <varname>expression</varname>
          </pt>
          <pd>An expression or value to assign to the corresponding column. </pd>
        </plentry>
        <plentry>
          <pt>DEFAULT</pt>
          <pd>The corresponding column will be filled with its default value.</pd>
        </plentry>
        <plentry>
          <pt>
            <varname>query</varname>
          </pt>
          <pd>A query (<codeph>SELECT</codeph> statement) that supplies the rows to be inserted.
            Refer to the <codeph>SELECT</codeph> statement for a description of the syntax.</pd>
        </plentry>
        <plentry>
          <pt><varname>output_expression</varname></pt>
          <pd>An expression to be computed and returned by the <codeph>INSERT</codeph> command after
            each row is inserted. The expression can use any column names of the table. Write * to
            return all columns of the inserted row(s).</pd>
        </plentry>
        <plentry>
          <pt><varname>output_name</varname></pt>
          <pd>A name to use for a returned column.</pd>
        </plentry>
      </parml>
    </section>
    <section id="section6"><title>Notes</title><p>To insert data into a partitioned table, you
        specify the root partitioned table, the table created with the <codeph>CREATE TABLE</codeph>
        command. You also can specify a leaf child table of the partitioned table in an
          <codeph>INSERT</codeph> command. An error is returned if the data is not valid for the
        specified leaf child table. Specifying a child table that is not a leaf child table in the
          <codeph>INSERT</codeph> command is not supported. Execution of other DML commands such as
          <codeph>UPDATE</codeph> and <codeph>DELETE</codeph> on any child table of a partitioned
        table is not supported. These commands must be run on the root partitioned table, the
        table created with the <codeph>CREATE TABLE</codeph> command.</p>For a partitioned table,
      all the child tables are locked during the <codeph>INSERT</codeph> operation when the Global
      Deadlock Detector is not enabled (the default). Only some of the leaf child tables are locked
      when the Global Deadlock Detector is enabled. For information about the Global Deadlock
      Detector, see <xref href="../../admin_guide/dml.html#topic_gdd" format="html" scope="external">Global Deadlock Detector</xref>.<p>For append-optimized
        tables, Greenplum Database supports a maximum of 127 concurrent <codeph>INSERT</codeph>
        transactions into a single append-optimized table. </p><p>For writable S3 external tables,
        the <codeph>INSERT</codeph> operation uploads to one or more files in the configured S3
        bucket, as described in <xref href="../../admin_guide/external/g-s3-protocol.html#amazon-emr"
        format="html" scope="external">s3:// Protocol</xref>. Pressing <codeph>Ctrl-c</codeph> cancels the <codeph>INSERT</codeph> and stops uploading
        to S3. </p></section>
    <section id="section7">
      <title>Examples</title>
      <p>Insert a single row into table <codeph>films</codeph>: </p>
      <codeblock>INSERT INTO films VALUES ('UA502', 'Bananas', 105, 
'1971-07-13', 'Comedy', '82 minutes');</codeblock>
      <p>In this example, the <codeph>length</codeph> column is omitted and therefore it will have
        the default value:</p>
      <codeblock>INSERT INTO films (code, title, did, date_prod, kind) VALUES 
('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');</codeblock>
      <p>This example uses the <codeph>DEFAULT</codeph> clause for the <codeph>date_prod</codeph>
        column rather than specifying a value: </p>
      <codeblock>INSERT INTO films VALUES ('UA502', 'Bananas', 105, DEFAULT, 
'Comedy', '82 minutes');</codeblock>
      <p>To insert a row consisting entirely of default values: </p>
      <codeblock>INSERT INTO films DEFAULT VALUES;</codeblock>
      <p>To insert multiple rows using the multirow <codeph>VALUES</codeph> syntax: </p>
      <codeblock>INSERT INTO films (code, title, did, date_prod, kind) VALUES
    ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'),
    ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy');</codeblock>
      <p>This example inserts some rows into table <codeph>films</codeph> from a table
          <codeph>tmp_films</codeph> with the same column layout as <codeph>films</codeph>: </p>
      <codeblock>INSERT INTO films SELECT * FROM tmp_films WHERE date_prod &lt; 
'2004-05-07';</codeblock>
      <p>Insert a single row into table distributors, returning the sequence number generated by the
          <varname>DEFAULT</varname>
        clause:<codeblock>INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets')
   RETURNING did;</codeblock></p>
    </section>
    <section id="section8">
      <title>Compatibility</title>
      <p><codeph>INSERT</codeph> conforms to the SQL standard. The case in which a column name list
        is omitted, but not all the columns are filled from the <codeph>VALUES</codeph> clause or
        query, is disallowed by the standard. </p>
      <p>Possible limitations of the <varname>query</varname> clause are documented under
          <codeph>SELECT</codeph>.</p>
    </section>
    <section id="section9">
      <title>See Also</title>
      <p><codeph><xref href="COPY.xml#topic1" type="topic" format="dita"/></codeph>, <codeph><xref
            href="./SELECT.xml#topic1" type="topic" format="dita"/></codeph>, <codeph><xref
            href="CREATE_EXTERNAL_TABLE.xml#topic1" type="topic" format="dita"/></codeph>, <xref
          href="../../admin_guide/external/g-s3-protocol.html#amazon-emr" format="html" scope="external">s3:// Protocol</xref></p>
    </section>
  </body>
</topic>
